package com.yjd.comm.system.util;

import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.system.model.AppMenuDto;
import com.yjd.comm.system.model.AppMenuParamDto;
import com.yjd.comm.system.model.MarketParamModel;
import com.yjd.comm.system.service.IMarketParamService;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.ServiceHolder;
import com.yjd.comm.util.StringUtil;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by yjd-wap on 2017/4/28.
 */
public class AppMenusUtils {
    private static final String SERVICE = "marketParamService";
    public final static String APPMENUPARAM = "app_menu_param";

    public static final String JYJL_KEY = "JYJL_KEY";//交易记录
    public static final String WDLB_KEY = "WDLB_KEY";//我的礼包
    public static final String ZDTB_KEY = "ZDTB_KEY";//自动投标
    public static final String WDLTK_KEY = "WDLTK_KEY";//我的灵通卡
    public static final String CGZH_KEY = "CGZH_KEY";//存管账户

    public static final String YHKGL_KEY = "YHKGL_KEY";//银行卡管理
    public static final String WDCGZH_KEY = "WDCGZH_KEY";//我的存管账户
    public static final String JYMM_KEY = "JYMM_KEY";//交易密码
    public static final String SMRZ_KEY = "SMRZ_KEY";//实名认证
    public static final String WDCGZH_WAP_URL_KEY = "WDCGZH_WAP_URL";//我的存管账户wap地址key


    private static IMarketParamService getMarketParamService() {
        return ServiceHolder.getLocalService(IMarketParamService.class, SERVICE);
    }

    /**
     * 获取APP菜单参数配置信息
     *
     * @return
     * @throws Exception
     */
    public static AppMenuParamDto getAppMenuParam() throws Exception {
        AppMenuParamDto menuParamDto = new AppMenuParamDto();
        MarketParamModel paramModel = getMarketParamService().getModelOne(FrameUtil.newHashMap("param_key", APPMENUPARAM), DbWREnums.READ);
        if (paramModel != null) {
            String paramValues = paramModel.getParam_value();
            if (StringUtil.isNotEmpty(paramValues)) {
                menuParamDto = StringUtil.getObject(paramValues, AppMenuParamDto.class);
                menuParamDto.setAppMenuList(getMenuSortList(menuParamDto.getAppMenuList()));
                menuParamDto.setMySetList(getMenuSortList(menuParamDto.getMySetList()));
            }
        }
        return menuParamDto;
    }


    /**
     * 获取九宫格菜单列表，已排序
     * @return
     * @throws Exception
     */
    public  static  List<AppMenuDto> getAppMenuList()throws  Exception{
        AppMenuParamDto paramDto =   getAppMenuParam();
        if (paramDto.getAppMenuList()!=null){
            return getMenuSortList(paramDto.getAppMenuList());
        }
        return null;
    }

    /**
     * 获取我的设置菜单列表，已排序
     * @return
     * @throws Exception
     */
    public  static  List<AppMenuDto> getMySetList()throws  Exception{
        AppMenuParamDto paramDto =   getAppMenuParam();
        if (paramDto.getMySetList()!=null){
            return getMenuSortList(paramDto.getMySetList());
        }
        return null;
    }


    /**
     * 菜单排序
     * @param appMenuList
     * @return
     * @throws Exception
     */
    public static List<AppMenuDto> getMenuSortList(List<AppMenuDto> appMenuList) throws Exception {
        Collections.sort(appMenuList, new Comparator<AppMenuDto>() {
            /*
             * int compare(Student o1, Student o2) 返回一个基本类型的整型，
             * 返回负数表示：o1 小于o2，
             * 返回0 表示：o1和o2相等，
             * 返回正数表示：o1大于o2。
             */
            public int compare(AppMenuDto o1, AppMenuDto o2) {
                //按照升序排列
                if (o1.getSort() > o2.getSort()) {
                    return 1;
                }
                if (o1.getSort() == o2.getSort()) {
                    return 0;
                }
                return -1;
            }
        });
        return appMenuList;
    }
}
